/*
 * @Author: didididijiajiajia 11643763+didididijiajiajia@user.noreply.gitee.com
 * @Date: 2023-08-22 13:04:13
 * @LastEditors: didididijiajiajia 11643763+didididijiajiajia@user.noreply.gitee.com
 * @LastEditTime: 2023-09-02 21:08:35
 * @FilePath: \vue低代码101-111\low_code\src\utils\useFocus.js
 */
import { computed, ref } from 'vue'
export function useFocus(data,previewRef=null, callback=null) {
    const selectIndex = ref(-1) //表示没有一个被选中
    //最后选择的block
    let lastSelectBlock = computed(() => data.value.block[selectIndex.value])
//    let lastSelectBlock =computed({
//     get(){
//         return data.value.block[selectIndex.value]
//     },
//     // set(){
//     //     return data.value.block[selectIndex.value]
//     // }
//    })

    //1.菜单拖拽
    //2.多个拖拽
    // 清空focus
    const clearBlockFocus = () => {
        data.value.block.forEach(block => block.focus = false)
    }
    const blockMouseDown = (e, block, index) => {
            if(previewRef.value) return 
        //阻止冒泡
        e.preventDefault()
        e.stopPropagation()
        if (e.ctrlKey) {
            block.focus = !block.focus
        } else {
            if (!block.focus) {
                // 清空所有的focus
                clearBlockFocus()
                block.focus = true
            }
        }
        selectIndex.value = index
        callback(e)

    }
    //点击外面区域，清空focus
    const containerMousedown = () => {
        if(previewRef.value) return 
        selectIndex.value = -1
        clearBlockFocus()
    }
    //获取被选中的元素

    const focusData = computed(() => {
        let focus = [];
        let unfocus = []
        data.value.block.forEach(block => (block.focus ? focus : unfocus).push(block))
        // debugger;
        return { focus, unfocus }
    })
 
    return {
        blockMouseDown, containerMousedown, focusData, lastSelectBlock,clearBlockFocus

    }
}